from sqlalchemy import Column, Integer, String, Date, DateTime

from model.sqlConnect import Base, SqliteSqlalchemy


class Goods(Base):
    # 定义表名
    __tablename__ = 'GOODS'
    # 定义字段
    id_goods = Column(Integer, autoincrement="auto", primary_key=True)
    id_dict = Column(String(20))
    code = Column(String(20))
    name = Column(String(50))
    desc = Column(String(20))
    icon_url = Column(String(50))
    image_url = Column(String(20))
    price = Column(Integer)
    stock = Column(Integer)
    is_deleted = Column(String(2), default="N")
    create_at = Column(DateTime, onupdate="nowDate")
    update_at = Column(DateTime, onupdate="nowDate")

    def nowDate(self):
        self.update_at = DateTime()
        self.create_at = DateTime()

    def queryAll(self):
        session = SqliteSqlalchemy().session
        result = session.query(Goods).all()
        session.close()
        return result


    def insert(self):
        session = SqliteSqlalchemy().session
        session.add(self)
        session.commit()
        session.close()
